```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树直径计算解析 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background: #2d2d2d;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .code-header {
            background: #1e1e1e;
            padding: 0.5rem 1rem;
            color: #9e9e9e;
            font-size: 0.9rem;
            display: flex;
            justify-content: space-between;
        }
        .code-body {
            padding: 1rem;
            color: #f8f8f2;
            overflow-x: auto;
        }
        .hover-scale {
            transition: transform 0.3s ease;
        }
        .hover-scale:hover {
            transform: scale(1.02);
        }
        .section-divider {
            height: 1px;
            background: linear-gradient(to right, transparent, rgba(0,0,0,0.1), transparent);
            margin: 2rem 0;
        }
        .concept-card {
            border-left: 4px solid #6e8efb;
            transition: all 0.3s ease;
        }
        .concept-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center justify-between">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6">二叉树直径计算解析</h1>
                    <p class="text-xl opacity-90 mb-8">深入探索二叉树最长路径的计算方法与实现原理</p>
                    <div class="flex items-center space-x-4">
                        <span class="flex items-center"><i class="fas fa-code text-lg mr-2"></i> 算法解析</span>
                        <span class="flex items-center"><i class="fas fa-project-diagram text-lg mr-2"></i> 数据结构</span>
                        <span class="flex items-center"><i class="fas fa-calculator text-lg mr-2"></i> 递归实现</span>
                    </div>
                </div>
                <div class="md:w-2/5">
                    <div class="bg-white bg-opacity-10 p-6 rounded-xl backdrop-blur-sm border border-white border-opacity-20">
                        <div class="mermaid">
                            graph TD
                            A[1] --> B[2]
                            A --> C[3]
                            B --> D[4]
                            B --> E[5]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Problem Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 hover-scale">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-question-circle text-blue-500 mr-3"></i>
                    问题定义
                </h2>
                <p class="text-lg text-gray-700 mb-4">
                    我们需要计算二叉树中任意两个节点之间路径的最大长度（也称为二叉树的直径）。这个路径可以通过二叉树中的节点数（节点的数量）或者通过边的数量（节点之间的连接）来表示。在这里，<span class="font-semibold text-blue-600">边的数量更符合"路径长度"的定义</span>。
                </p>
                <div class="flex flex-wrap gap-4 mt-6">
                    <div class="bg-blue-50 text-blue-800 px-4 py-2 rounded-full flex items-center">
                        <i class="fas fa-bullseye mr-2"></i>
                        <span>二叉树遍历</span>
                    </div>
                    <div class="bg-purple-50 text-purple-800 px-4 py-2 rounded-full flex items-center">
                        <i class="fas fa-sitemap mr-2"></i>
                        <span>递归算法</span>
                    </div>
                    <div class="bg-green-50 text-green-800 px-4 py-2 rounded-full flex items-center">
                        <i class="fas fa-project-diagram mr-2"></i>
                        <span>深度计算</span>
                    </div>
                </div>
            </div>
        </section>

        <!-- Core Idea -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>
                核心思想
            </h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="concept-card bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-arrow-down text-blue-500 mr-2"></i>
                        递归深度计算
                    </h3>
                    <p class="text-gray-700">
                        二叉树的直径可以通过递归遍历二叉树来求解。在遍历的过程中，我们需要计算每个节点的"左子树深度"和"右子树深度"，并通过这些深度计算出通过该节点的最大路径长度。
                    </p>
                </div>
                <div class="concept-card bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-sync-alt text-purple-500 mr-2"></i>
                        动态更新最大值
                    </h3>
                    <p class="text-gray-700">
                        这个路径长度就是"左子树深度 + 右子树深度 + 1"或者"左子树深度 + 右子树深度"，取决于你如何定义路径长度。我们需要在递归过程中不断更新这个最大值。
                    </p>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-chart-line text-green-500 mr-3"></i>
                算法可视化
            </h2>
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="mermaid">
                    graph TD
                    subgraph 递归过程
                    A[1] -- depth=3 --> B[计算最大直径]
                    A --> C[左子树深度=2]
                    A --> D[右子树深度=1]
                    C --> E[节点2]
                    D --> F[节点3]
                    E --> G[左子树深度=1]
                    E --> H[右子树深度=1]
                    G --> I[节点4]
                    H --> J[节点5]
                    end
                </div>
                <div class="mt-6 p-4 bg-blue-50 rounded-lg">
                    <h4 class="font-semibold text-blue-800 mb-2 flex items-center">
                        <i class="fas fa-info-circle mr-2"></i>
                        可视化解读
                    </h4>
                    <p class="text-blue-700">
                        图中展示了递归计算过程，在每个节点处计算左右子树的深度，并在节点1处获得最大直径3（路径4→2→1→3）。
                    </p>
                </div>
            </div>
        </section>

        <!-- Implementation Steps -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-tasks text-red-500 mr-3"></i>
                实现步骤
            </h2>
            <div class="space-y-6">
                <div class="flex items-start">
                    <div class="bg-blue-500 text-white rounded-full w-8 h-8 flex items-center justify-center flex-shrink-0 mr-4 mt-1">1</div>
                    <div>
                        <h3 class="text-xl font-semibold mb-2 text-gray-800">定义递归函数</h3>
                        <p class="text-gray-700">这个递归函数不仅要返回当前节点的深度，还要在递归过程中更新最大路径长度。</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="bg-blue-500 text-white rounded-full w-8 h-8 flex items-center justify-center flex-shrink-0 mr-4 mt-1">2</div>
                    <div>
                        <h3 class="text-xl font-semibold mb-2 text-gray-800">计算子树深度</h3>
                        <p class="text-gray-700">对于每个节点，递归计算它的左子树和右子树的深度。</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="bg-blue-500 text-white rounded-full w-8 h-8 flex items-center justify-center flex-shrink-0 mr-4 mt-1">3</div>
                    <div>
                        <h3 class="text-xl font-semibold mb-2 text-gray-800">更新最大路径长度</h3>
                        <p class="text-gray-700">通过计算"左子树深度 + 右子树深度"来更新最大路径长度。</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="bg-blue-500 text-white rounded-full w-8 h-8 flex items-center justify-center flex-shrink-0 mr-4 mt-1">4</div>
                    <div>
                        <h3 class="text-xl font-semibold mb-2 text-gray-800">返回深度</h3>
                        <p class="text-gray-700">最后，函数返回当前节点的深度，以便上层节点可以继续计算。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Example -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-code text-indigo-500 mr-3"></i>
                代码实现
            </h2>
            <div class="code-block">
                <div class="code-header">
                    <span>BinaryTreeDiameter.java</span>
                    <span><i class="far fa-copy"></i></span>
                </div>
                <div class="code-body">
                    <pre><code class="language-java">class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int val) {
        this.val = val;
    }
}

public class TreeDiameterDemo {
    private int maxDiameter = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return maxDiameter;
    }

    private int depth(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int leftDepth = depth(node.left);
        int rightDepth = depth(node.right);

        // 更新最大直径
        maxDiameter = Math.max(maxDiameter, leftDepth + rightDepth);

        // 返回当前节点的深度
        return Math.max(leftDepth, rightDepth) + 1;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);

        TreeDiameterDemo demo = new TreeDiameterDemo();
        int diameter = demo.diameterOfBinaryTree(root);
        System.out.println("二叉树的最大路径长度为: " + diameter);  // 输出应该是 3
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Explanation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-comment-alt text-teal-500 mr-3"></i>
                代码解析
            </h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-function text-purple-500 mr-2"></i>
                        depth函数
                    </h3>
                    <p class="text-gray-700 mb-4">
                        这是一个递归函数，用于计算树的深度。通过递归调用左子树和右子树的深度，更新全局变量<code class="bg-gray-100 px-1 py-0.5 rounded">maxDiameter</code>，它记录了目前为止找到的最大路径长度。
                    </p>
                    <div class="p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <span class="font-semibold">关键点:</span> 递归终止条件是节点为空时返回0，然后自底向上计算每个节点的深度。
                        </p>
                    </div>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-database text-blue-500 mr-2"></i>
                        maxDiameter变量
                    </h3>
                    <p class="text-gray-700 mb-4">
                        它存储着二叉树中任意两个节点之间的最大路径长度，并在每次递归中不断更新。最终<code class="bg-gray-100 px-1 py-0.5 rounded">diameterOfBinaryTree</code>方法返回这个最大值。
                    </p>
                    <div class="p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <span class="font-semibold">注意:</span> 最大直径可能不经过根节点，这就是为什么需要在递归过程中不断更新最大值。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Result Section -->
        <section class="mb-16 bg-white rounded-xl shadow-md p-8 hover-scale">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-check-circle text-green-500 mr-3"></i>
                执行结果
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-green-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-green-800">输入二叉树结构</h3>
                    <div class="mermaid">
                        graph TD
                        1 --> 2
                        1 --> 3
                        2 --> 4
                        2 --> 5
                    </div>
                </div>
                <div class="bg-blue-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-blue-800">最长路径</h3>
                    <p class="text-2xl font-bold text-blue-600 mb-2">4 → 2 → 1 → 3</p>
                    <p class="text-gray-700">共经过3条边</p>
                </div>
                <div class="bg-purple-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-purple-800">程序输出</h3>
                    <div class="bg-black bg-opacity-80 rounded p-4">
                        <code class="text-white">二叉树的最大路径长度为: 3</code>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索编程之美，解析算法精髓</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>www.yuque.com/jtostring</span>
                    </a>
                </div>
            </div>
            <div class="mt-8 pt-8 border-t border-gray-800 text-center text-gray-500 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Add hover effect to code copy button
        document.querySelectorAll('.code-header').forEach(header => {
            const copyBtn = header.querySelector('i');
            copyBtn.addEventListener('click', () => {
                const code = header.nextElementSibling.querySelector('code').textContent;
                navigator.clipboard.writeText(code).then(() => {
                    copyBtn.classList.remove('far', 'fa-copy');
                    copyBtn.classList.add('fas', 'fa-check');
                    setTimeout(() => {
                        copyBtn.classList.remove('fas', 'fa-check');
                        copyBtn.classList.add('far', 'fa-copy');
                    }, 2000);
                });
            });
        });
    </script>
</body>
</html>
```